package cn.net.xyan.easy.webapp.base.config;

import cn.net.xyan.easy.spring.web.config.MVCApplicationContextConfig;
import cn.net.xyan.easy.webapp.base.service.ISecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * Created by xiashenpin on 16/2/9.
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    ISecurityService securityService;

    @Autowired
    public void configureGlobal( AuthenticationManagerBuilder auth) throws Exception {
//        auth
//                .inMemoryAuthentication()
//                .withUser("user").password("password").roles("USER");


        auth.userDetailsService(securityService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/static/**", String.format("%s**", MVCApplicationContextConfig.WEBJARROOT), "/context","/dependence.js").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll();
        http
                .logout()
                //.logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                //.logoutSuccessHandler(logoutSuccessHandler)
                .invalidateHttpSession(true) ;
    }
}
